package com.doublethread.easymart.modules.user.controller;

import com.doublethread.easymart.common.context.UserContext;
import com.doublethread.easymart.common.result.Result;
import com.doublethread.easymart.modules.user.dto.AddressDTO;
import com.doublethread.easymart.modules.user.service.AddressService;
import com.doublethread.easymart.modules.user.vo.UserAddressVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/user/addresses")
public class UserAddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 获取地址列表
     */
    @GetMapping
    public Result<List<UserAddressVO>> getAddressList() {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取用户地址列表");

        List<UserAddressVO> addressList = addressService.getUserAddressList(userId);

        log.info("获取用户地址列表成功");
        return Result.success(addressList);
    }

    /**
     * 添加收货地址
     */
    @PostMapping
    public Result<UserAddressVO> createAddress(@Valid @RequestBody AddressDTO addressDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("添加地址");

        UserAddressVO addressVO = addressService.createAddress(userId, addressDTO);

        log.info("添加地址成功");
        return Result.success(addressVO);
    }

    /**
     * 获取地址详情
     */
    @GetMapping("/{id}")
    public Result<UserAddressVO> getAddressById(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取地址详情");

        UserAddressVO addressVO = addressService.getAddressById(userId, id);

        log.info("获取地址详情成功");
        return Result.success(addressVO);
    }

    /**
     * 更新收货地址
     */
    @PutMapping("/{id}")
    public Result<Void> updateAddress(@PathVariable Long id,
                                      @Valid @RequestBody AddressDTO addressDTO) {
        Long userId = UserContext.getCurrentUserId();

        log.info("更新地址");

        addressService.updateAddress(userId, id, addressDTO);

        log.info("更新地址成功");
        return Result.success();
    }

    /**
     * 删除收货地址
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteAddress(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUserId();

        log.info("删除地址");

        addressService.deleteAddress(userId, id);

        log.info("删除地址成功");
        return Result.success();
    }

    /**
     * 获取默认地址
     */
    @GetMapping("/default")
    public Result<UserAddressVO> getDefaultAddress() {
        Long userId = UserContext.getCurrentUserId();

        log.info("获取默认地址");

        UserAddressVO addressVO = addressService.getDefaultAddress(userId);

        log.info("获取默认地址成功");
        return Result.success(addressVO);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/{id}/default")
    public Result<Void> setDefaultAddress(@PathVariable Long id) {
        Long userId = UserContext.getCurrentUserId();

        log.info("设置默认地址");

        addressService.setDefaultAddress(userId, id);

        log.info("设置默认地址成功");
        return Result.success();
    }
}
